/**
 * @author wangC
 * @create 2020-06-30 下午11:24
 * @Description  synchronized
 *
 * synchronized 底层Hotspot的实现是在 对象的头上拿出两位来记录是否有锁，并记录锁的类型
 *
 **/
public class Synchronized_1 {


    private static int count = 10 ;
    private Object o = new Object();


    public void m() {
        synchronized (o) { //任何线程要执行下面的代码，必须先拿o的锁
            count--;
            System.out.println(Thread.currentThread().getName() + "count = " + count );
        }
    }

    public void n() {
        synchronized (this) {  ////任何线程要执行下面的代码，必须先拿到this的锁
            count--;
            System.out.println(Thread.currentThread().getName() + "count = " + count );
        }
    }


    public synchronized void m_1() { //等同于在方法的代码执行时要synchronized(this)
        count--;
        System.out.println(Thread.currentThread().getName() + " count = " + count);
    }

    public synchronized static void m_2() { //这里等同于synchronized(Synchronized_1.class)
        count--;
        System.out.println(Thread.currentThread().getName() + " count = " + count);
    }

    public static void m_3() {
        synchronized(Synchronized_1.class) { //考虑一下这里写synchronized(this)是否可以？
            count --;
        }
    }




}
